package com.lanou.controller;

import com.lanou.bean.Blog;
import com.lanou.service.BlogService;
import com.lanou.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by dllo on 18/2/26.
 */
@Controller
public class BlogController {
    @Resource
    private BlogService blogService;
    @Resource
    UserService userService;

    @RequestMapping(value = "blog")
    public String blog(){
        return "blog";
    }

    @RequestMapping(value = "add")
    public String add() {
        return "addblog";
    }


    @RequestMapping(value = "addblog")
    public String addBlog(Blog blog,HttpSession session) {
        /*Blog blog = new Blog();
        blog.setTitle(title);
        blog.setDes(des);
        blog.setContent(content);*/
        String name=session.getAttribute("user").toString();
        Integer uid = userService.selectuid(name).getId();
        System.out.println(uid);
        blog.setUserId(uid);
        blogService.add(blog);
        return "blog";
    }

    @RequestMapping(value = "del")
    public String del(HttpSession session) {
        String name=session.getAttribute("user").toString();
        Integer uid = userService.selectuid(name).getId();
        blogService.del(uid);
        return "blog";
    }

    @ResponseBody
    @RequestMapping(value = "deleteblog")
    public String deleteblog(@RequestParam("id") Integer id) {
        blogService.del(id);
        return "blog";
    }
    @ResponseBody
    @RequestMapping(value = "select")
    public List<Blog> select(){
        return blogService.selectAll();
    }
}
